package com.example.wyt.recycleviewtest.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wyt.recycleviewtest.DescActivity;
import com.example.wyt.recycleviewtest.R;
import com.example.wyt.recycleviewtest.entity.PeopleEntity;

import java.util.List;

public class PeopleListAdapter extends RecyclerView.Adapter<PeopleListAdapter.PeopleListHolder> {
    private Context context;
    private List<PeopleEntity> mPeopleEntities;

    static class PeopleListHolder extends ViewHolder{
        ImageView vwIma;
        TextView vwName;
        Button vwClick;

        public PeopleListHolder(@NonNull View itemView) {
            super(itemView);
            vwIma = itemView.findViewById(R.id.vw_item_ima);
            vwName = itemView.findViewById(R.id.vw_item_name);
            vwClick = itemView.findViewById(R.id.vw_item_click);
        }
    }

    public PeopleListAdapter(Context context,List<PeopleEntity> peopleEntities){
        this.context = context;
        mPeopleEntities = peopleEntities;
    }

    @NonNull
    @Override
    public PeopleListHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_recycle,viewGroup,false);
        return new PeopleListHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PeopleListHolder peopleListHolder, final int i) {
        final PeopleEntity peopleEntity = mPeopleEntities.get(i);
        peopleListHolder.vwIma.setImageResource(peopleEntity.getImaId());
        peopleListHolder.vwName.setText(peopleEntity.getName());

        peopleListHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,DescActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("name",peopleEntity.getName());
                bundle.putString("desc",peopleEntity.getDesc());
                bundle.putInt("imaId",peopleEntity.getImaId());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mPeopleEntities.size();
    }
}
